package com.mazaiting.lab.service.impl;

import com.mazaiting.web.domain.base.BasePageQuery;
import com.mazaiting.common.core.utils.DateTimeUtil;
import com.mazaiting.lab.domain.vo.BucketVO;
import com.mazaiting.lab.service.IMinioFileService;
import com.mazaiting.minio.ex.MinioException;
import com.mazaiting.minio.service.IMinioService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * minio 文件服务实现
 *
 * @author mazaiting
 * @create_time 2022/10/8 17:11
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IMinioFileServiceImpl implements IMinioFileService {

    private final IMinioService minioService;

    @Override
    public BasePageVO<BucketVO> listBuckets(BasePageQuery basePageQuery) throws MinioException {
        int pageNum = basePageQuery.getPageNum();
        int pageSize = basePageQuery.getPageSize();
        // 分页查询
        List<BucketVO> bucketVOList = minioService.getAllBuckets()
                .stream()
                .skip((long) (pageNum - 1) * pageSize).limit(pageSize)
                .map(bucket -> new BucketVO(bucket.name(), DateTimeUtil.time2String(bucket.creationDate().toLocalDateTime())))
                .toList();
        // 构建分页对象
        BasePageVO<BucketVO> basePageVO = new BasePageVO<>();
        basePageVO.setTotal((long) bucketVOList.size());
        basePageVO.setList(bucketVOList);
        return basePageVO;
    }
}
